
#include <jni.h>
#include "ar_opencv.h"

#define JAVA_METHOD(method) Java_cn_ubeta_ar_jni_JniUtils_##method

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JAVA_METHOD(arInit)(JNIEnv *env, jclass arg, jbyteArray data, jint width, jint height) {
    LOGE("arInit width %d, height %d", width, height);
    jbyte *yuvData = env->GetByteArrayElements(data, 0);
    Mat yuvImg(height + height / 2, width, CV_8UC1, (uchar *) yuvData);
    arInit(yuvImg);
}

JNIEXPORT jintArray JAVA_METHOD(arTracking)(JNIEnv *env, jclass arg, jbyteArray data, jint width, jint height) {
    const int len = 8;
    int points[len];
    jbyte *yuvData = env->GetByteArrayElements(data, 0);
    Mat yuvImg(height + height / 2, width, CV_8UC1, (uchar *) yuvData);

    if (arTracking(yuvImg, width, height, points)) {
        jintArray bounds = env->NewIntArray(len);
        // 返回边缘点
        env->SetIntArrayRegion(bounds, 0, len, points);
        return bounds;
    } else {
        return NULL;
    }
}

#ifdef __cplusplus
}
#endif
